package com.hrma.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrma.dao.EmployeeMapper;
import com.hrma.dao.RewardMapper;
import com.hrma.entity.Reward;
import com.hrma.entity.RewardDTO;
import com.hrma.entity.SelectResult;
import com.hrma.services.RewardService;
import com.hrma.utils.UserUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RewardImpl implements RewardService {
    private final RewardMapper rewardMapper;
    private final EmployeeMapper employeeMapper;

    public RewardImpl(RewardMapper rewardMapper, EmployeeMapper employeeMapper) {
        this.rewardMapper = rewardMapper;
        this.employeeMapper = employeeMapper;
    }

    @Override
    public Reward add(Reward data) {
        data.setAuditor(employeeMapper.getNumById(UserUtil.getUserId()));
        rewardMapper.add(data);
        return data;
    }

    @Override
    public void delete(int id) {
        if (!rewardMapper.exist(id)) {
            throw new RuntimeException("id字段不存在！");
        }
        rewardMapper.delete(id);
    }

    @Override
    public void update(Reward data) {
        if (!rewardMapper.exist(data.getId())) {
            throw new RuntimeException("id字段不存在！");
        }
        rewardMapper.update(data);
    }

    @Override
    public List<SelectResult> select() {
        return rewardMapper.select();
    }

    @Override
    public Page<Reward> getList(RewardDTO data) {
        data.setState(1);
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return rewardMapper.getList(data);
    }
}
